//Adventuring Adventurer's Adventurous Adventure
#include <string>
#include <stdio.h>
#include <ctime>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <ctype.h>
#include "pystring.h"
#include "combat.h"
#include "player.h"
#include "enemy.h"

using namespace std;

bool is_number(const std::string& s)
{
   for (int i = 0; i < s.length(); i++) {
       if (!std::isdigit(s[i]))
           return false;
   }

   return true;
}

void creation(player &currPlayer)
{
	string stringTemp;
	int intTemp;
	bool wasCorrect = false;
	char accept;

	do
	{
		cout << "Character name:\n>> ";
		cin >> stringTemp;
		//20 Chars, Spaces
		if (std::string::npos != stringTemp.find_first_of("0123456789-"))
		{
			cout << "digit(s)found!" << endl;
			stringTemp = "";
			wasCorrect = false;
			cout << "Incorrect Input, please enter a name without numbers..." << endl;
		}
		else if(stringTemp.length() <= 20)
		{
			currPlayer.setName(stringTemp);
			wasCorrect = true;
		}
		else
		{
			stringTemp = "";
			wasCorrect = false;
			cout << "Incorrect Input, please enter a name less than 20 characters..." << endl << endl;
		}
	}while(wasCorrect == false);

	wasCorrect = false;

	do
	{
		cout << "\nAge:\n>> ";
		cin >> stringTemp;
		//Any input will do so long as its int
		if(is_number(stringTemp))
		{
			currPlayer.setAge(atoi(stringTemp.c_str()));
			wasCorrect = true;
		}
		else
		{
			stringTemp = "";
			wasCorrect = false;
			cout << "Incorrect Input, please only use numbers..." << endl;
		}
	}while(wasCorrect == false);

	wasCorrect = false;

	do
	{
		cout << "\nGender:\n>> ";
		cin >> stringTemp;
		//Male or Female ONLY (Male, Female, M, F)
		if(stringTemp == "M" || "F" || "Male" || "Female")
		{
			currPlayer.setGender(stringTemp);
			wasCorrect = true;
		}
		else
		{
			stringTemp = "";
			wasCorrect = false;
			cout << "Incorrect Input, please only enter M, F, Male or Female..." << endl;
		}
	}while(wasCorrect == false);

	wasCorrect = false;

	cout << "\nRace:\n>> ";
	cin >> stringTemp;
	//Any input will do
	currPlayer.setRace(stringTemp);

	do
	{
		cout << "Character eye colour:\n>> ";
		cin >> stringTemp;
		//20 Chars, Spaces
		if (std::string::npos != stringTemp.find_first_of("0123456789-"))
		{
			cout << "digit(s)found!" << endl;
			stringTemp = "";
			wasCorrect = false;
			cout << "Incorrect Input, please enter a name without numbers..." << endl;
		}
		else if(stringTemp.length() <= 20)
		{
			currPlayer.setEyeColour(stringTemp);
			wasCorrect = true;
		}
		else
		{
			stringTemp = "";
			wasCorrect = false;
			cout << "Incorrect Input, please enter a colour less than 20 characters..." << endl << endl;
		}
	}while(wasCorrect == false);

	wasCorrect = false;

	do
	{
		cout << "Character hair colour:\n>> ";
		cin >> stringTemp;
		//20 Chars, Spaces
		if (std::string::npos != stringTemp.find_first_of("0123456789-"))
		{
			cout << "digit(s)found!" << endl;
			stringTemp = "";
			wasCorrect = false;
			cout << "Incorrect Input, please enter a name without numbers..." << endl;
		}
		else if(stringTemp.length() <= 20)
		{
			currPlayer.setHairColour(stringTemp);
			wasCorrect = true;
		}
		else
		{
			stringTemp = "";
			wasCorrect = false;
			cout << "Incorrect Input, please enter a colour less than 20 characters..." << endl << endl;
		}
	}while(wasCorrect == false);

	wasCorrect = false;

	do
	{
		cout << "Character skin colour:\n>> ";
		cin >> stringTemp;
		//20 Chars, Spaces
		if (std::string::npos != stringTemp.find_first_of("0123456789-"))
		{
			cout << "digit(s)found!" << endl;
			stringTemp = "";
			wasCorrect = false;
			cout << "Incorrect Input, please enter a name without numbers..." << endl;
		}
		else if(stringTemp.length() <= 20)
		{
			currPlayer.setSkinColour(stringTemp);
			wasCorrect = true;
		}
		else
		{
			stringTemp = "";
			wasCorrect = false;
			cout << "Incorrect Input, please enter a colour less than 20 characters..." << endl << endl;
		}
	}while(wasCorrect == false);

	wasCorrect = false;

	cout << endl;
	cout << "Your character:\n" << currPlayer.getName() << "\n" << currPlayer.getAge() << "\n" << currPlayer.getGender() << "\n" << currPlayer.getRace() << "\n" << currPlayer.getEyeColour() << "\n" << currPlayer.getHairColour << "\n" << currPlayer.getSkinColour() <<"\nIs this information correct? Y/N\n>> ";
	cin >> accept;
	cout << endl;

	if(accept == 'y')
	{
		ofstream characterSheet; //Creates a new txt file holding new player information
		characterSheet.open(currPlayer.getName() + ".txt");
		characterSheet << currPlayer.getName() << "\n" << currPlayer.getAge() << "\n" << currPlayer.getGender() << "\n" << currPlayer.getRace() << "\n" << currPlayer.getEyeColour() << "\n" << currPlayer.getHairColour << "\n" << currPlayer.getSkinColour() << endl;
		characterSheet.close();

		//ofstream characterSheet; //Saves the player name to the master list of player names (assists in loading existing players)
		characterSheet.open("characterSheet.txt", fstream::app);
		characterSheet << currPlayer.getName() << endl;
		characterSheet.close();
	}
	else
		cout << "Character taken to farm; you will never hear from them.\n" << endl;
}

void load(player &currPlayer)
{
	string line;
	string players[4];
	int numPlayer = 0;
	int loadSel = 0;


	ifstream characterSheet("characterSheet.txt");
	if(characterSheet.is_open())
	{
		while(characterSheet.good())
		{
			getline(characterSheet,line);
			if(numPlayer < 4)
				players[numPlayer++] = line;
		}

		characterSheet.close();
	}
	else 
		cout << "Unable to open file"; 


	cout << "Please select saved player: " << endl;

	for (int i = 0; i < 4; i++)
	{
		cout << i + 1 << ": " << players[i] << endl;
	}
	cin >> loadSel;

	switch(loadSel)
	{
		case 1:
			{
				currPlayer.readPlayerData(players[0]);
				break;
			}
		case 2:
			{
				currPlayer.readPlayerData(players[1]);
				break;
			}
		case 3:
			{
				currPlayer.readPlayerData(players[2]);
				break;
			}
		case 4:
			{
				currPlayer.readPlayerData(players[3]);
				break;
			}
		default:
			break;
	}
}

void debugMode(player &currPlayer)
{
	cout << "Your character:\n" << currPlayer.getName() << "\n" << currPlayer.getAge() << "\n" << currPlayer.getGender() << "\n" << currPlayer.getRace() << endl;
}

int main()
{
	char mainOption;
	player *currPlayer = new player();
	enemy *currEnemy = new enemy();
	combat *combatSim;
	
	
	srand(time(0));
	cout << setprecision(4);

	cout << "Welcome to Adventuring Adventurer's Adventurous Adventure!\n" << endl;
	cout << "1. New Game\n2. Load Game\n3. Combat Test\n4. Quit\n>> ";
	cin >> mainOption;
	cout << endl;

	while (mainOption != '4')
	{
		switch(mainOption)
		{
			case '1':
				creation(*currPlayer);
				break;
			case '2':
				load(*currPlayer);
				break;
			case '3':
				currEnemy->readPlayerData("Grue");
				combatSim = new combat(*currPlayer, *currEnemy);
				combatSim->battle();
				break;
			case'5':
				debugMode(*currPlayer);
				break;
			default:
				cout << "Not yet Implemented.\n" << endl;
				break;
		}

		cout << "Welcome to Adventuring Adventurer's Adventurous Adventure!\n" << endl;
		cout << "1. New Game\n2. Load Game\n3. Combat Test\n4. Quit\n>> ";
		cin >> mainOption;
		cout << endl;
	}
	cout << "Farewell, adventurer..." << endl;
	
	delete combatSim;
	delete currPlayer;

	system("pause");
	return 0;
}